import Vue  from 'vue'
import VueRouter from 'vue-router'

const Login = () => import(/* webpackChunkName: "Login_Home_Wellcome" */ '../components/Login.vue')
const Home = () => import(/* webpackChunkName: "Login_Home_Wellcome" */ '../components/Home.vue')
const Wellcome = () => import(/* webpackChunkName: "Login_Home_Wellcome" */ '../components/home/Wellcome.vue')


const Users = () => import(/* webpackChunkName: "Users_Right_Roles" */ '../components/home/Users')
const Right = () => import(/* webpackChunkName: "Users_Right_Roles" */ '../components/power/Right')
const Roles = () => import(/* webpackChunkName: "Users_Right_Roles" */ '../components/power/Roles')

const Cate = () => import(/* webpackChunkName: "Cate_Params_Goods_Add" */ '../components/goods/Cate')
const Params = () => import(/* webpackChunkName: "Cate_Params_Goods_Add" */ '../components/goods/Params')
const Goods = () => import(/* webpackChunkName: "Cate_Params_Goods_Add" */ '../components/goods/Goods')
const Add = () => import(/* webpackChunkName: "Cate_Params_Goods_Add" */ '../components/goods/Add')


const Orders = () => import(/* webpackChunkName: "Orders_Report" */ '../components/orders/Orders')
const Report = () => import(/* webpackChunkName: "Orders_Report" */ '../components/report/Report')



Vue.use(VueRouter)

const routes = [
  // 重定向
  { path: '/', redirect: '/login'},
  { path: '/login', component: Login},
  { 
    path: '/home', 
    component: Home, 
    redirect: '/wellcome',
    children: [
      { path: '/wellcome', component: Wellcome},
      { path: '/users', component: Users},
      { path: '/rights', component: Right},
      { path: '/roles', component: Roles},
      { path: '/categories', component: Cate},
      { path: '/params', component: Params},
      { path: '/goods', component: Goods },
      { path: '/goods/add', component: Add},
      { path: '/orders', component: Orders},
      { path: '/reports', component: Report}
    ]
  },
  
]

const router = new VueRouter({
  routes
})

// 挂在路由导航守卫
router.beforeEach((to, from, next) => {
  //访问地址是登录页面,直接放行
  if (to.path == "/login") return next()
  //不是登录界面, 验证是否有sessionStorage
  const token = window.sessionStorage.getItem('token')
  if (!token) return  next("/login")
  next()

})
export default router
